import Vue from 'vue'
import App from './App'
import store from './store'
import my from './common/config.js'
import JMessage from 'lib/jmessage-wxapplet-sdk-1.4.2.min.js'
// import http from './common/request.js'
Vue.config.productionTip = false

App.mpType = 'app'

var http = my.http;

Vue.prototype.$http = http

// #ifndef MP-TOUTIAO
var jim = new JMessage({});
let data = {}
http.post('/api/v1.0/message/WebIMAuthentication', data, res => {
	if (res.data.status == '000000') {
		let resData = JSON.parse(res.data.data)
		uni.setStorage({
			key: "appkey",
			data: resData.appkey
		})
		jim.init({
			"appkey": resData.appkey,
			"random_str": resData.random_str,
			"signature": resData.signature,
			"timestamp": resData.timestamp,
			'flag': 1
		}).onSuccess(function(data) {
			//初始化成功
			if (data.message == 'success') {
				//login
				//如果登录有账户 直接登录im
				if (uni.getStorageSync('userId')) {
					let userId = uni.getStorageSync('userId')
					jim.login({
						'username': 'buyer' + userId,
						'password': userId
					}).onSuccess(function(data) {

					}).onFail(function(data) {
						console.log(data)
						//同上
					});

				}
			}


		}).onFail(function(data) {
			//TODO
		});
	} else {
		uni.showToast({
			title: res.data.message,
			icon: 'none'
		})
	}
})
Vue.prototype.$jim = jim
//#endif





Vue.prototype.$store = store
// Vue.prototype.$url = 




const app = new Vue({
	store,
	...App
})
app.$mount()
